package Server;

import java.net.*;
import java.io.*;
import java.lang.*;

// declaration of class NetworkScoket
class networksocket {

	Socket socket;
	BufferedReader in;
	BufferedWriter out;
	String name = new String("Guest"); // default name for the user
	int id = 1;

	// Constructor function that takes a socket as a parameter
	networksocket(Socket client) {
		// get the streams
		try {
			in = new BufferedReader(new InputStreamReader(client
					.getInputStream()));
			out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
			socket = client;
		} catch (IOException e) {
		}
	}

	// Takes a string and puts it on the outgoing stream, then flushes the
	// stream
	public void write_stream(String s) { // write to the socket

		try {
			out.write(s);
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // write the string to the socket
		System.out.println("writing out " + s);
	}

	// Takes an input stream, returns the string
	public String read_stream() { // returns the data to get

		try {
			String inputStream = in.readLine(); // get a line
			System.out.println("read this " + inputStream);
			return inputStream;
		} catch (IOException e) {
		}
		return "null"; // return the read in string
	}

	// returns whether or not the client can read from the socket
	public int is_readable() { // returns if can read from this socket

		boolean test;

//		if (out.checkError()) { // return -1 if error to close connection
//			System.out.println("Error: Client unreachable");
//			return -1;
//		}
//		try {
//			test = (in.available() > 1); // return true if can read
//		} catch (IOException e) {
//			return -1;
//		}// close the connection if can't test
//		if (test) // 1 if can read
//			return 1;
//		else
//			// 0 if can't
			return 0;
	}

	// closes everything down
	public void kill() { // kill everything

		try {
			if (out != null)
				out.close();
			if (in != null)
				in.close();
			if (socket != null)
				socket.close();
		} catch (IOException e) {
		}

	}

}
